home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Apache 1.0 / src / mod_mime.c < prev    next >
Text File  |  1995-12-04  |  8KB  |  258 lines

  1.  
  2. /* ====================================================================
  3.  * Copyright (c) 1995 The Apache Group.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer. 
  11.  *
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in
  14.  *    the documentation and/or other materials provided with the
  15.  *    distribution.
  16.  *
  17.  * 3. All advertising materials mentioning features or use of this
  18.  *    software must display the following acknowledgment:
  19.  *    "This product includes software developed by the Apache Group
  20.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  21.  *
  22.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  23.  *    endorse or promote products derived from this software without
  24.  *    prior written permission.
  25.  *
  26.  * 5. Redistributions of any form whatsoever must retain the following
  27.  *    acknowledgment:
  28.  *    "This product includes software developed by the Apache Group
  29.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  30.  *
  31.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  32.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  35.  * IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  * ====================================================================
  44.  *
  45.  * This software consists of voluntary contributions made by many
  46.  * individuals on behalf of the Apache Group and was originally based
  47.  * on public domain software written at the National Center for
  48.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  49.  * For more information on the Apache Group and the Apache HTTP server
  50.  * project, please see <http://www.apache.org/>.
  51.  *
  52.  */
  53.  
  54.  
  55. /*
  56.  * http_mime.c: Sends/gets MIME headers for requests
  57.  * 
  58.  * Rob McCool
  59.  * 
  60.  */
  61.  
  62. #define MIME_PRIVATE
  63.  
  64. #include "httpd.h"
  65. #include "http_config.h"
  66.  
  67. typedef struct {
  68.     table *forced_types;    /* Additional AddTyped stuff */
  69.     table *encoding_types;    /* Added with AddEncoding... */
  70.     table *language_types;    /* Added with AddLanguage... */
  71. } mime_dir_config;
  72.  
  73. module mime_module;
  74.  
  75. void *create_mime_dir_config (pool *p, char *dummy)
  76. {
  77.     mime_dir_config *new =
  78.       (mime_dir_config *) palloc (p, sizeof(mime_dir_config));
  79.  
  80.     new->forced_types = make_table (p, 4);
  81.     new->encoding_types = make_table (p, 4);
  82.     new->language_types = make_table (p, 4);
  83.     
  84.     return new;
  85. }
  86.  
  87. void *merge_mime_dir_configs (pool *p, void *basev, void *addv)
  88. {
  89.     mime_dir_config *base = (mime_dir_config *)basev;
  90.     mime_dir_config *add = (mime_dir_config *)addv;
  91.     mime_dir_config *new =
  92.       (mime_dir_config *)palloc (p, sizeof(mime_dir_config));
  93.  
  94.     new->forced_types = overlay_tables (p, add->forced_types,
  95.                     base->forced_types);
  96.     new->encoding_types = overlay_tables (p, add->encoding_types,
  97.                       base->encoding_types);
  98.     new->language_types = overlay_tables (p, add->language_types,
  99.                       base->language_types);
  100.  
  101.     return new;
  102. }
  103.  
  104. char *add_type(cmd_parms *cmd, mime_dir_config *m, char *ct, char *ext)
  105. {
  106.     if (*ext == '.') ++ext;
  107.     table_set (m->forced_types, ext, ct);
  108.     return NULL;
  109. }
  110.  
  111. char *add_encoding(cmd_parms *cmd, mime_dir_config *m, char *enc, char *ext)
  112. {
  113.     if (*ext == '.') ++ext;
  114.     table_set (m->encoding_types, ext, enc);
  115.     return NULL;
  116. }
  117.  
  118. char *add_language(cmd_parms *cmd, mime_dir_config *m, char *lang, char *ext)
  119. {
  120.     if (*ext == '.') ++ext;
  121.     table_set (m->language_types, ext, lang);
  122.     return NULL;
  123. }
  124.  
  125. /* The sole bit of server configuration that the MIME module has is
  126.  * the name of its config file, so...
  127.  */
  128.  
  129. char *set_types_config (cmd_parms *cmd, void *dummy, char *arg)
  130. {
  131.     set_module_config (cmd->server->module_config, &mime_module,
  132.                pstrdup (cmd->pool, arg));
  133.     return NULL;
  134. }
  135.  
  136. command_rec mime_cmds[] = {
  137. { "AddType", add_type, NULL, OR_FILEINFO, ITERATE2,
  138.     "a mime type followed by one or more file extensions" },
  139. { "AddEncoding", add_encoding, NULL, OR_FILEINFO, ITERATE2,
  140.     "an encoding (e.g., gzip), followed by one or more file extensions" },
  141. { "AddLanguage", add_language, NULL, OR_FILEINFO, ITERATE2,
  142.     "a language (e.g., fr), followed by one or more file extensions" },
  143. { "TypesConfig", set_types_config, NULL, RSRC_CONF, TAKE1,
  144.     "the MIME types config file" },
  145. { NULL }
  146. };
  147.  
  148. /* Hash table  --- only one of these per daemon; virtual hosts can
  149.  * get private versions through AddType...
  150.  */
  151.  
  152. #define MIME_HASHSIZE 27
  153. #define hash(i) (isalpha(i) ? (tolower(i)) - 'a' : 26)
  154.  
  155. static table *hash_buckets[MIME_HASHSIZE];
  156.  
  157. void init_mime (server_rec *s, pool *p)
  158. {
  159.     FILE *f;
  160.     char l[MAX_STRING_LEN];
  161.     int x;
  162.     char *types_confname = get_module_config (s->module_config, &mime_module);
  163.  
  164.     if (!types_confname) types_confname = TYPES_CONFIG_FILE;
  165.  
  166.     types_confname = server_root_relative (p, types_confname);
  167.  
  168.     if(!(f = fopen(types_confname,"r"))) {
  169.         fprintf(stderr,"httpd: could not open mime types file %s\n",
  170.                 types_confname);
  171.         perror("fopen");
  172.         exit(1);
  173.     }
  174.  
  175.     for(x=0;x<27;x++) 
  176.         hash_buckets[x] = make_table (p, 10);
  177.  
  178.     while(!(cfg_getline(l,MAX_STRING_LEN,f))) {
  179.         char *ll = l, *ct;
  180.       
  181.         if(l[0] == '#') continue;
  182.         ct = getword_conf (p, &ll);
  183.  
  184.         while(ll[0]) {
  185.             char *ext = getword_conf (p, &ll);
  186.         str_tolower (ext);    /* ??? */
  187.         table_set (hash_buckets[hash(ext[0])], ext, ct);
  188.         }
  189.     }
  190.     fclose(f);
  191. }
  192.  
  193. int find_ct(request_rec *r)
  194. {
  195.     int i;
  196.     char *fn = pstrdup (r->pool, r->filename);
  197.     mime_dir_config *conf =
  198.       (mime_dir_config *)get_module_config(r->per_dir_config, &mime_module);
  199.     char *type;
  200.  
  201.     if (S_ISDIR(r->finfo.st_mode)) {
  202.         r->content_type = DIR_MAGIC_TYPE;
  203.     return OK;
  204.     }
  205.     
  206.     if((i=rind(fn,'.')) < 0) return DECLINED;
  207.     ++i;
  208.  
  209.     if ((type = table_get (conf->encoding_types, &fn[i])))
  210.     {
  211.         r->content_encoding = type;
  212.  
  213.     /* go back to previous extension to try to use it as a language */
  214.     
  215.         fn[i-1] = '\0';
  216.     if((i=rind(fn,'.')) < 0) return OK;
  217.     ++i;
  218.     }
  219.  
  220.     if ((type = table_get (conf->language_types, &fn[i])))
  221.     {
  222.         r->content_language = type;
  223.  
  224.     /* go back to previous extension to try to use it as a type */
  225.     
  226.         fn[i-1] = '\0';
  227.     if((i=rind(fn,'.')) < 0) return OK;
  228.     ++i;
  229.     }
  230.  
  231.     if ((type = table_get (conf->forced_types, &fn[i]))
  232.     || (type = table_get (hash_buckets[hash(fn[i])], &fn[i])))
  233.     {
  234.         r->content_type = type;
  235.     }
  236.     
  237.     return OK;
  238. }
  239.  
  240.  
  241. module mime_module = {
  242.    STANDARD_MODULE_STUFF,
  243.    init_mime,            /* initializer */
  244.    create_mime_dir_config,
  245.    merge_mime_dir_configs,
  246.    NULL,            /* server config */
  247.    NULL,            /* merge server config */
  248.    mime_cmds,
  249.    NULL,            /* handlers */
  250.    NULL,            /* filename translation */
  251.    NULL,            /* check_user_id */
  252.    NULL,            /* check auth */
  253.    NULL,            /* check access */
  254.    find_ct,            /* type_checker */
  255.    NULL,            /* fixups */
  256.    NULL                /* logger */
  257. };
  258.